import { Component, OnInit } from '@angular/core';
import { _HttpClient, SettingsService } from '@delon/theme';
import { NzNotificationService, NzMessageService } from 'ng-zorro-antd';

/**
 * 修改密保手机
 * security-phone.component
 */
@Component({
  selector: 'app-security-secphone',
  templateUrl: './security-phone.component.html',
})
export class SecurityPhoneComponent implements OnInit {
  userLoading = true;
  user;
  phone;
  password;
  code;
  inithide = true;
  showEdit = true;

  constructor(
    public http: _HttpClient,
    private notice: NzNotificationService,
    private msg: NzMessageService,
    private setting: SettingsService,
  ) {}

  ngOnInit() {
    this.init();
    // 如果已经有了则全部隐藏
    // 否则显示
  }

  init() {
    this.userLoading = true;
    this.http.get(`/cf/admin/user/${this.setting.user.userId}`).subscribe(res => {
      this.userLoading = false;
      this.user = res.data;
      this.inithide = this.user.phone ? true : false;
    });
  }

  save() {
    if (!this.phone && this.showEdit) {
      this.msg.warning('手机号码不能空');
      return;
    }
    if (!this.password) {
      this.msg.warning('登录密码不能空');
      return;
    }
    if (!this.code && this.showEdit) {
      this.msg.warning('短信验证码不能空');
      return;
    }

    const data = { ...this.user, phone: this.phone, password: this.password, captcha: this.code, bind: this.showEdit };
    this.http.put(`/cf/admin/user/bind`, data).subscribe(res => {
      if (res.status === 200) {
        this.notice.success('成功', `${this.showEdit ? '绑定' : '解绑'}密保手机成功`);
        this.init();
        this.showEdit = true;
      }
      this.phone = '';
      this.password = '';
      this.code = '';
    });
  }

  sendCaptcha() {
    this.http.get(`/cf/admin/mobile/${this.phone}`).subscribe(res => {
      console.log(res);
    });
  }
}
